unit Input;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,ZConnection, DB, ZAbstractRODataset, ZAbstractDataset, ZDataset;
type
    InputData = Class(TObject)  // This is an actual class definition :
    // Internal class field definitions - only accessible in this unit
    private

    // Fields and methods only accessible by this class and descendants
    protected
    // Externally accessible fields and methods
    public
      // 2 constructors - one for round fruit, the other long fruit
      constructor Create(diameter : single);               overload;
      constructor Create(length : single; width : single); overload;
      procedure insertQuery(ZConnection1:TZConnection;ZTable1:TZTable;ZQuery1:TZQuery;TableName:TStringList; values:TStringList);


    // Externally accessible and inspectable fields and methods
    published
      // Note that properties must use different names to local defs

  end;                    // End of the TFruit class definition

implementation
    // Create a round fruit object
  constructor InputData.Create(diameter: single);
  begin
    // Indicate that we have a round fruit, and set its size
  end;

  // Create a long fruit object
  constructor InputData.Create(length, width: single);
  begin
    // Indicate that we have a long fruit, and set its size
  end;

  procedure InputData.insertQuery(ZConnection1:TZConnection;ZTable1:TZTable;ZQuery1:TZQuery;TableName:TStringList; values:TStringList);
  var A:TStringList;
    K: Integer;
    table:String;

      sFile: String;
    sConnectionString: String;
    slTables: TStringList;
    i, j: Integer;
    fField: TField;
    sLine: String;
    sPath: TStrings;
    sTemp: TStrings;
    kota: String;
    kec: String;
    desa: String;
    idkota: String;
    kkota:String;
    idkec: String;
    idesa: String;
    ktemp:String;
    tanggal:String;
    idtgl:String;
    vallist: String;
    qQue:String;
    tulis : Boolean;
    stata : Integer;
  begin
    kota := values[12];
    kec := values[13];
    desa:= values[14];
    idesa := TableName[2];
    //INSERT tanggal
    tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
    with ZQuery1 do  //masukkin nih gan
      begin
        Active:=False;
        sql.Clear;
        sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
        ExecSQL;
      end;
    ZTable1.Active := False;
    ZTable1.TableName := 'tanggal';
    ZTable1.SortedFields:='ID_TANGGAL';
    ZTable1.SortType := stDescending;
    ZTable1.Active := True;
    ZTable1.First;
    idtgl := ZTable1.Fields[1].AsString;
    //ShowMessage(idtgl);
    ZTable1.SortedFields:='';
    //create table
    ZQuery1.Connection := ZConnection1;
    ZQuery1.SQL.Clear;
    table:='pemilih_'+idesa;
    ZQuery1.SQL.Add('CREATE TABLE IF NOT EXISTS `'+table+'` ('+
    '`ID_USER` int(11) NOT NULL AUTO_INCREMENT,'+
    '`NIK` char(36) NOT NULL,'+
    '`NKK` char(36) NOT NULL,'+
    '`NAMA` char(50) NOT NULL,'+
    '`TEMPAT` char(36) NOT NULL,'+
    '`TANGGAL` char(10) NOT NULL,'+
    '`IBU` char(36) NOT NULL,'+
    '`AGAMA` char(36) NOT NULL,'+
    '`KAWIN` char(1) NOT NULL,'+
    '`KELAMIN` char(1) NOT NULL,'+
    '`KELAINAN` char(36) NOT NULL,'+
    '`ALAMAT` char(150) NOT NULL,'+
    '`RW` char(3) NOT NULL,'+
    '`RT` char(3) NOT NULL,'+
    '`DUSUN` char(30) NOT NULL,'+
    '`KET` char(150) NOT NULL,'+
    '`ID_TANGGAL` int(11) NOT NULL,'+
    'TPS int(11) NOT NULL,'+
    'STATUS1 char(1) NOT NULL,'+
    'STATUS2 char(1) NOT NULL,'+
    'GANDA char(1) NOT NULL,'+
    'PRIMARY KEY (`ID_USER`)'+
    ') ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;');
      ZQuery1.ExecSQL;

      //insert isi
    ZQuery1.SQL.Clear;
    qQue:='INSERT INTO `'+table+'` (`NIK`, `NKK`, `NAMA`, `TEMPAT`, `TANGGAL`, '+
    '`IBU`, `AGAMA`, `KAWIN`, `KELAMIN`, `KELAINAN`, `ALAMAT`, `RW`, `RT`, `DUSUN`, `KET`, '+
    '`ID_TANGGAL`, TPS, STATUS1, STATUS2, GANDA) VALUES'+
    ' ('+QuotedStr(values[1])+','+QuotedStr(values[2])+','+QuotedStr(values[3])+
    ','+QuotedStr(values[4])+','+QuotedStr(values[5])+','+QuotedStr(values[6])+
    ','+QuotedStr(values[7])+','+QuotedStr(values[8])+','+QuotedStr(values[9])+
    ','+QuotedStr(values[10])+','+QuotedStr(values[11])+','+QuotedStr(values[15])+
    ','+QuotedStr(values[16])+','+QuotedStr(values[17])+','+QuotedStr(values[18])+','+idtgl+',0,"1","0","0")';
    ZQuery1.SQL.Add(qQue);
    ZQuery1.ExecSQL;
      //cek ada tidak
    ZTable1.Active := False;
    ZTable1.TableName := 'penyimpan';
    ZTable1.Filter := 'NAMA = '+QuotedStr(table);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    if (ZTable1.FieldByName('NAMA').AsString = '') then
      begin
        with ZQuery1 do  //masukkin nih gan
          begin
            Active:=False;
            sql.Clear;
            sql.Text := 'INSERT IGNORE INTO penyimpan (NAMA) VALUES ("'+table+'")';
            ExecSQL;
          end;
      end;
    //cek tabel pemilih
    ZTable1.Active := False;
    ZTable1.TableName := table;
    ZTable1.Filter := 'NIK = '+QuotedStr(values[1])+' and NKK='+QuotedStr(values[2])+' and NAMA='+QuotedStr(values[3]);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    //indexing
    values[0] := ZTable1.Fields[0].AsString;
    //insertIndexing(values);
  end;
end.
